#!/bin/sh

#-------------------------------------------------------------------------------
# restore.sh
# 
# This shell script is invoked to perform a restore of the HSC backup data.
# This script should be executed every time the system reboots.  If no
# "indicator" file is present, the script exits and no file restoration is done.
# 
# Usage: 'restore'
#
# Return Codes:
# 1 - Error mounting the media
# 2 - Error Copying from the media
# 3 - Archive not present
# 4 - Error unmounting the media
# 5 - Other Errors
# 6 - DVD drive failure
# 7 - No media inserted
# 8 - Unformatted media
# 9 - Incorrect label on media
# 10 - tar error
# 11 - wrong actbrsp
# 12 - no need to run a restore

#-------------------------------------------------------------------------------
# directory and filename which records the backup actions.
#-------------------------------------------------------------------------------
LOGDIR=/var/hsc/log
LOG=$LOGDIR/hscrestore.log
   
LOG_ERROR_LOG=/tmp/hscrestore.log

# STDIO directed to /tmp/mount_output + PID
MOUNT_OUTPUT=/tmp/mount_output$$

#-------------------------------------------------------------------------------
# mount point: the mount point where the backup data is contained
#-------------------------------------------------------------------------------
MOUNTPOINT=/mnt/cdrom

#-------------------------------------------------------------------------------
# temporary working directory
#-------------------------------------------------------------------------------
TMPDIR=/tmp/restore

#-------------------------------------------------------------------------------
# the filename of the backup archive
#-------------------------------------------------------------------------------
ARCHIVENAME=backuphdr.tgz
ARCHIVE=$MOUNTPOINT/$ARCHIVENAME

#-------------------------------------------------------------------------------
# the (hard drive) media mount point where the indicator file, created by the  
# CD re-install process, is stored.  There should be an entry for this
# mountpoint in /etc/fstab
#-------------------------------------------------------------------------------
UPGRADE_MOUNTPOINT=/mnt/upgrade

#-------------------------------------------------------------------------------
# the indicator file. If it exists, execute 'tar' cmd to restore the backup data
#-------------------------------------------------------------------------------
IQYBCRIT=$UPGRADE_MOUNTPOINT/iqybcrit.dat


# common exit point for script
exit_cleanup() {
    rm -f $MOUNT_OUTPUT
    umount -v $UPGRADE_MOUNTPOINT
    exit $1
}  



# Check if the directory for the log file exists.
if [ ! -d $LOGDIR ]; then
	echo "=================================================================" > $LOG_ERROR_LOG
	echo -e "Restore task log for `date`." >> $LOG_ERROR_LOG
	echo "Restore task log directory, <$LOGDIR>, does not exist. Program exiting" >> $LOG_ERROR_LOG
	exit 5
fi

# Start log to record restore actions.
echo -e "Restore log for `date`\n" > $LOG


#-------------------------------------------------------------------------------
# Mount the media where the indicator file should be located
#-------------------------------------------------------------------------------
mount -v $UPGRADE_MOUNTPOINT > $MOUNT_OUTPUT 2>&1
if [ $? -ne 0 ] ; then

   #----------------------------------------------------------------------------
   # already mounted?  Not an error, but a non-zero return code.  Continue
   #----------------------------------------------------------------------------
   if grep "already mounted" $MOUNT_OUTPUT; then
      echo "The mountpoint, $UPGRADE_MOUNTPOINT, is already mounted.  Continuing..." >> $LOG

   #----------------------------------------------------------------------------
   # other error?  Game over.
   #----------------------------------------------------------------------------
   else
      echo "The mountpoint, $UPGRADE_MOUNTPOINT, cannot be mounted.  Error is $?" >> $LOG
      exit_cleanup 1
   fi
   
fi

#-------------------------------------------------------------------------------
# good mount of output device, continue
#-------------------------------------------------------------------------------
echo "partition with indicator file, $IQYBCRIT, mounted at $UPGRADE_MOUNTPOINT." >> $LOG


#-------------------------------------------------------------------------------
# if IQYBCRIT.DAT doesn't exist, there is nothing to do
#-------------------------------------------------------------------------------
if [ ! -e $IQYBCRIT ]; then
      echo "$IQYBCRIT indicator file does not exist. Data restoration not required" >> $LOG
      echo "restore script exiting." >> $LOG
      exit_cleanup 12
fi


#-------------------------------------------------------------------------------
# Check to see if the backup media (DVD) is already mounted
#-------------------------------------------------------------------------------
if grep -q "$MOUNTPOINT" /etc/mtab; then # Media is already mounted
	if ! umount -v $MOUNTPOINT >> $LOG 2>&1; then
		echo "Couldn't umount the media.. exiting" >> $LOG
		exit_cleanup 4
	fi
fi

#-------------------------------------------------------------------------------
# Mount and check for existence of archive
#-------------------------------------------------------------------------------
#if mount -t udf -v $MOUNTPOINT > $MOUNT_OUTPUT 2>&1; then   # udf/dvd specific
if mount -v $MOUNTPOINT > $MOUNT_OUTPUT 2>&1; then
	echo -e "successfully mounted the backup media\n" >> $LOG
        
# leave this test out since HSC does not do a specific DVD format
#       if !(chkudf /dev/hdc | grep -q -i "ACTBKP" 2>&1); then # udf/dvd specific
	false=0
        if false; then # floppy test
        	echo "The media has an incorrect label.. exiting" >> $LOG
                umount $MOUNTPOINT
                exit_cleanup 9
	elif [ ! -e $ARCHIVE ]; then
		echo "The backup archive is not on the restore media.. exiting" >> $LOG
		umount $MOUNTPOINT
		exit_cleanup 3
        fi
else
	echo -e "Couldn't Mount the media.. \n" >> $LOG
        if grep -q -i "wrong major or minor number" $MOUNT_OUTPUT; then
        	echo -e "Hardware failure on the DVD RAM drive.. exiting\n" >> $LOG
                exit_cleanup 6
        elif grep -q -i "No medium found" $MOUNT_OUTPUT; then
        	echo -e "No media in the DVD RAM drive.. exiting\n" >> $LOG
                exit_cleanup 7
        elif grep -q -i "wrong fs type" $MOUNT_OUTPUT; then
        	echo -e "Wrong filesystem type, or unformatted media, or wrong media type.. exiting\n" >> $LOG
                exit_cleanup 8
        else
		echo -e "Unknown error.. exiting\n" .>> $LOG
       		exit_cleanup 1
        fi
fi


#-------------------------------------------------------------------------------
# Now do the actual restore from the archive
#-------------------------------------------------------------------------------
echo -e "starting restore...\n" >> $LOG
echo -e "command issued is 'tar -xvzPf $ARCHIVE --overwrite'\n" >> $LOG

if !(tar -xvzPf $ARCHIVE --overwrite >> $LOG 2>&1); then
        echo -e "tar error, $?, extracting files from archive.. exiting\n" >> $LOG
        umount $MOUNTPOINT
        exit_cleanup 10
else
        echo -e "restore from archive successful.. \n" >> $LOG
        #-----------------------------------------------------------------------
        # remove the indicator file
        #-----------------------------------------------------------------------
        rm -f $IQYBCRIT
fi

#-------------------------------------------------------------------------------
# unmount the restore media
#-------------------------------------------------------------------------------
if umount -v $MOUNTPOINT >> $LOG 2>&1; then
	echo "done." >> $LOG
	exit_cleanup 0
else
	echo "Couldn't umount the media" >> $LOG
	exit_cleanup 4
fi
